//
//  YDHBasePopView.m
//  YunDaHome-iOS
//
//  Created by yunda on 2020/6/8.
//  Copyright © 2020 YunDa. All rights reserved.
//

#import "YDHBasePopView.h"

@interface YDHBasePopView(){
    CGAffineTransform dismissTransform;
}

@end

@implementation YDHBasePopView

-  (instancetype)init{
    self = [super init];
    if (!self) {
        return nil;
    }
    // init code
    self.frame = CGRectMake(0, 0, J_SCREEN_WIDTH, J_SCREEN_HEIGHT);
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.1];
    
    [self addSubview:self.viewBg];
            
    return self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches.allObjects lastObject];
    if (![touch.view isDescendantOfView:self.viewBg]) {
        [self dismiss];
    }
}

- (void)show{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    [UIView animateWithDuration:0.25 animations:^{
        self.viewBg.transform = CGAffineTransformMakeScale(1.0, 1.0);
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    }];
}

- (void)dismiss{
    [UIView animateWithDuration:0.25 animations:^{
        self.viewBg.transform = self->dismissTransform;
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    } completion:^(BOOL finished) {
        if (finished) [self removeFromSuperview];
    }];
}

#pragma mark--SETTER
- (void)setAnimationType:(PopAnimationType)animationType{
    _animationType = animationType;
    if (animationType == PopAnimationType_Top) {
        [_viewBg j_setAnchorPoint:CGPointMake(0.5, 0.0)];
        dismissTransform = CGAffineTransformMakeScale(1.0, 0.00001);
    }else if (animationType == PopAnimationType_Center){
        [_viewBg j_setAnchorPoint:CGPointMake(0.5, 0.5)];
        dismissTransform = CGAffineTransformMakeScale(0.00001, 0.00001);
    }else if (animationType == PopAnimationType_Bottom){
        [_viewBg j_setAnchorPoint:CGPointMake(0.5, 1.0)];
        dismissTransform = CGAffineTransformMakeScale(1.0, 0.00001);
    }
    _viewBg.transform = dismissTransform;
}

#pragma mark--GETTER
- (UIView *)viewBg{
    if (!_viewBg) {
        _viewBg = [[UIView alloc]init];
        _viewBg.backgroundColor = [UIColor whiteColor];
    }
    return _viewBg;
}

@end
